package fr.upstlse.glre.client.administration.document.libelle;

import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.administration.component.Center;
import fr.upstlse.glre.client.administration.document.libelle.action.CreateLibelleAction;
import fr.upstlse.glre.client.constante.AdminConstante;
import fr.upstlse.glre.client.constante.RfgConstanteEnum;
import fr.upstlse.glre.client.widgets.fields.LibelleFields;

public class CreationLibellePanel extends Composite {
	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	private ContentPanel creationPanel;

	private LibelleFields libelleFields;
	private FormPanel formPanel;
	
	private static CreationLibellePanel instance;

	public CreationLibellePanel() {
		CreationLibellePanel.setInstance(this);
		
		createCreationPanel();
		createCreationForm();

		creationPanel.add(formPanel);
		initWidget(creationPanel);
	}

	private void createCreationPanel() {
		creationPanel = new ContentPanel();
		creationPanel.setAutoWidth(true);
		creationPanel.setWidth(500);
		creationPanel.setId(AdminConstante.LIBELLE_CREATION_PANEL_ID.getValue());
		creationPanel.setBorders(true);
		creationPanel.setCollapsible(true);

		// Draggable panel
		Draggable lDraggable = new Draggable(creationPanel, creationPanel
				.getHeader());
		lDraggable.setContainer(Center.getInstance());

		creationPanel
				.setHeading(AdminConstante.LIBELLE_CREATION_PANEL_HEADER_TEXT.getValue());
	}

	public void createCreationForm() {
		formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setAutoWidth(true);

		libelleFields = new LibelleFields(null, formPanel);

		Button lSubmit = new Button(
				RfgConstanteEnum.CREATION_SUBMIT.getValue());
		lSubmit.addSelectionListener(new CreateLibelleAction());
		formPanel.addButton(lSubmit);
	}

	public ContentPanel getCreationPanel() {
		return creationPanel;
	}

	public void setCreationPanel(ContentPanel creationPanel) {
		this.creationPanel = creationPanel;
	}
	
	public static CreationLibellePanel getInstance() {
		if(instance == null) {
			instance = new CreationLibellePanel();
		}
		
		return instance;
	}

	public static void setInstance(CreationLibellePanel pCreationLibellePanel) {
		CreationLibellePanel.instance = pCreationLibellePanel;
	}

	public FormPanel getFormPanel() {
		return formPanel;
	}

	public void setFormPanel(FormPanel pFormPanel) {
		formPanel = pFormPanel;
	}

	public LibelleFields getLibelleFields() {
		return libelleFields;
	}

	public void setLibelleFields(LibelleFields libelleFields) {
		this.libelleFields = libelleFields;
	}
}
